//
//  YXViewController.swift
//  Cell动画弹跳
//
//  Created by Rookie_YX on 16/7/18.
//  Copyright © 2016年 YX_Rookie. All rights reserved.
//

import UIKit

class YXViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
  
  var boundsArr = [CGRect]()
  var newPath:NSIndexPath?
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // TableView
    view.addSubview(tableView)
  }
  
  private lazy var tableView:UITableView = {
    
    let tab = UITableView(frame: self.view.bounds, style:.Plain)
    tab.delegate = self
    tab.dataSource = self
    tab.separatorStyle = UITableViewCellSeparatorStyle.None
    return tab
  }()
  
  private lazy var imageInfo:[String] = {
    var imgInfo:[String] = []
    for i in 1...7{
      imgInfo.append("\(i).jpg")
    }
    return imgInfo
  }()
}

// MARK - UITableViewDelegate,UITableViewDataSource
extension YXViewController{
  
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    return self.imageInfo.count ?? 0
  }
  
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    
    let identifier:String = "SetUpTableViewCell"+String(indexPath.section) + String(indexPath.row)
    
    if indexPath.row == newPath?.row {    // 中间cell
      var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? YXLableCell
      
      if cell == nil {
        cell = YXLableCell.init(style:.Default, reuseIdentifier: identifier, frame: CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 200))
      }
      cell?.imgV.image = UIImage(named: self.imageInfo[indexPath.row])
      return cell!
    }
    else{   // 其他cell
      var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? YXCell
      
      if cell == nil {
        cell = YXCell.init(style:.Default, reuseIdentifier: identifier, frame: CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 200))
      }
      cell?.imgV.image = UIImage(named: self.imageInfo[indexPath.row])
      return cell!
    }
  }
  
  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 200
  }
  
  func scrollViewDidScroll(scrollView: UIScrollView) {

    var point = view.center
    point = self.tableView.convertPoint(point, fromView: self.tableView.superview)
    newPath = self.tableView.indexPathForRowAtPoint(point)!
    print(newPath!.row)
    self.tableView.reloadData()
  }
}